<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/29
 * Time: 18:04
 */

namespace App\HttpController\Home;

use App\Enums\UrlEnums;
use App\Utility\Curl;
use EasySwoole\Http\Message\Status;
use EasySwoole\MysqliPool\Mysql;

class GameCompetitions extends Base
{
    public function all()
    {
        $params = $this->request()->getRequestParam();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 30;
        $db = Mysql::defer('mysql');
        if( isset($params['category']) && !empty($params['category']) ){
            $db = $db->where('category',$params['category']);
        }
        if( isset($params['state']) && !empty($params['state']) ){
            $db = $db->where('state',$params['state']);
        }
        $data = $db->get('siam_game_competitions',[$pageSize * ($page  - 1), $pageSize]);
        if( !empty($data) ){
            foreach ( $data as $k=>$v ){
                $data[$k] = $this->jsonToArr($v);
            }
        }
        return $this->writeJson(200,$data);
    }

    /**
     * 已完成
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function finish()
    {
        $params = $this->request()->getRequestParam();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 30;

        $db = Mysql::defer('mysql');
        if( isset($params['category']) && !empty($params['category']) ){
            $db = $db->where('category',$params['category']);
        }
        $data = $db->where('state','finish')->get('siam_game_competitions',[$pageSize * ($page  - 1), $pageSize]);
        if( !empty($data) ){
            foreach ( $data as $k=>$v ){
                $data[$k] = $this->jsonToArr($v);
            }
        }
        return $this->writeJson(200,$data);
    }

    /**
     * 正在进行中
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function start()
    {
        $params = $this->request()->getRequestParam();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 30;

        $db = Mysql::defer('mysql');
        if( isset($params['category']) && !empty($params['category']) ){
            $db = $db->where('category',$params['category']);
        }

        $data = $db->where('state','start')->get('siam_game_competitions',[$pageSize * ($page  - 1), $pageSize]);
        if( !empty($data) ){
            foreach ( $data as $k=>$v ){
                $data[$k] = $this->jsonToArr($v);
            }
            foreach ( $data as $k1=>$v1 ){
                $data[$k1] = $this->jsonToArr($v1);
//                if( $params['category'] == 'lol' ){
//                    $odds = $this->gameList($v1['left_team']['name'],$v1['right_team']['name']);
//                    $data[$k1]['match_odds'] = $odds;
//                }
            }
        }
        return $this->writeJson(200,$data);
    }

    /**
     * 即将开始
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function upcoming()
    {
        $params = $this->request()->getRequestParam();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 30;

        $db = Mysql::defer('mysql');
        if( isset($params['category']) && !empty($params['category']) ){
            $db = $db->where('category',$params['category']);
        }
        $data = $db->where('state','upcoming')->get('siam_game_competitions',[$pageSize * ($page  - 1), $pageSize]);
        if( !empty($data) ){
            foreach ( $data as $k=>$v ){
                $data[$k] = $this->jsonToArr($v);
            }
        }
        return $this->writeJson(200,$data);
    }

    /**
     * 获取单条数据
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function one()
    {
        $params = $this->request()->getRequestParam();
        if( !isset($params['id']) || empty($params['id']) ){
            return $this->writeJson(3001,[],'缺少必要参数！');
        }
        $db = Mysql::defer('mysql');
        $db->where('id',$params['id'])->getOne('siam_game_competitions');
    }

    /**
     * lol赛事详情
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function lolDetail()
    {
        $params = $this->request()->getRequestParam();
        if( !isset($params['_id']) || empty($params['_id']) ){
            return $this->writeJson(3001,[],'缺少必要参数！');
        }
        $db = Mysql::defer('mysql');
        $data = $db->where('_id',$params['_id'])->getOne('siam_lol_gamedetail');
        $data = $this->jsonToArr($data);
        return $this->writeJson(200,$data);
    }

    public function dota2Detail()
    {
        $params = $this->request()->getRequestParam();
        if( !isset($params['_id']) || empty($params['_id']) ){
            return $this->writeJson(3001,[],'缺少必要参数！');
        }
        $db = Mysql::defer('mysql');
        $data = $db->where('_id',$params['_id'])->getOne('siam_dota2_gamedetail');
        $data = $this->jsonToArr($data);
        return $this->writeJson(200,$data);
    }

    public function csgoDetail()
    {
        $params = $this->request()->getRequestParam();
        $limit = $params['limit'] ?? 20;
        if( !isset($params['_id']) || empty($params['_id']) ){
            return $this->writeJson(3001,[],'缺少必要参数！');
        }
        $url = "https://1zplay.com/api/csgo_data/{$params['_id']}?limit={$limit}&map=All";
        $data = Curl::request('GET',$url);
        return $this->response()->write($data);
    }

    public function getBetOdds()
    {
        $gets = $this->request()->getRequestParam();
        $url = UrlEnums::FANYA_GAMEODDS;
        $params = [
            'header' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ]
        ];

        $content = $this->handleCurl('GET',$url[0],$params);
        if( empty($content) ){
            $content = $this->handleCurl('POST',$url[1],$params);
        }

        foreach ( $content['info'] as $k=>$v ){
            if( $k == $gets['match_id'] ){
                return $v;
            }
        }
    }

    /**
     * 获取游戏列表
     */
    public function gameList($left =null,$right = null)
    {

        $url = UrlEnums::FANYA_GAMELISTS;
        $params = [
            'form_params' => [
                'Type'  => 'UpComing,Live',
                'CateId'=> 2,
            ],
        ];
        $content = $this->handleCurl('POST',$url[0],$params);
        if( empty($content) ){ //备用的连接
            $content = $this->handleCurl('POST',$url[1],$params);
        }

        foreach ( $content['info'] as $k=>$v ){
            if( $v['Player'][0]['Name'] == $left && $v['Player'][0]['Name'] == $right ){
                $left_odds = $this->getBetOdds($v['Player'][0]['MatchID']);
                $right_odds = $this->getBetOdds($v['Player'][0]['MatchID']);
                return ['left'=>$left_odds,'right'=>$right_odds];
            }
        }

        return ['left'=>null,'right'=>null];
    }
}